﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace WinFormUIXMLParser
{
    public class XmlButtonObject : XmlControlBase
    {
        public string Image { get; set; }

        public string Click { get; set; }
       
        public XmlButtonObject()
            : base()
        {

        }
 
        public override string ToXMLString()
        {
            throw new NotImplementedException();
        }

        public override Control ToControl<T>(T parent)
        {
            Button btn = new Button();
            SetValues(btn);            
            if (!string.IsNullOrEmpty(Click))
            {
                Type t = typeof(T);
                System.Reflection.MethodInfo methodInfo = t.GetMethod(Click, BindingFlags.Instance | BindingFlags.NonPublic);
                Delegate DelegateInstance = StaticTypes.CreateDelegateFromMethodInfoByDelegate<EventHandler>(parent, methodInfo);
                if (DelegateInstance != null)
                {
                    EventHandler handler = DelegateInstance as EventHandler;
                    btn.Click += handler;
                }          
            }          
            return btn;
        }
    }
}
